package com.wll.study.algotiyhm.about_linklode;


import com.wll.study.algotiyhm.about_linklode.ListNode;

/**
 * 难度： **
 * 返回链表中倒数第K个节点
 * https://leetcode-cn.com/problems/kth-node-from-end-of-list-lcci/
 */
public class GetLinkListLastKNode {
    /**
     * 使用双指针求倒数第K个节点
     * @param head 列表的头指针
     * @param k 倒数第k个节点
     * @return 倒数第k个节点对应的值
     *
     */
    public static int kthToLastTwoPoint(ListNode head, int k) {
        if (head == null){
            return 0;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k; i++) {
            fast = fast.next;
        }
        while (fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

}
